﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class productcatlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (null != Request.QueryString["c"])
            {
                InitTitle(Convert.ToInt32(Request.QueryString["c"].ToString()));
                InitLeftNav(Convert.ToInt32(Request.QueryString["c"].ToString()));
            }
            if (null != Request.QueryString["id"])
            {
                InitList(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
    }

    private void InitTitle(int c)
    {
        CProduct product = new CProduct();
        DataRow r = product.Read(c);
        this.ltlTitle.Text = r["title"].ToString();
    }

    private void InitLeftNav(int c)
    {
        CProduct product = new CProduct();
        ProductInfo[] ps = product.ReadCategoryTree(c);
        StringBuilder sb = new StringBuilder();
        if (null != ps)
        {
            if (null == Request.QueryString["id"])
            {
                InitList(ps[0].ID);
            }
            for (int i = 0; i < ps.Length; i++)
            {
                sb.Append("<a href=\"productcatlist.aspx?c=" + c.ToString() + "&id=" + ps[i].ID.ToString() + "\">" + ps[i].Title + "</a>");
                if (null != ps[i].Children)
                {
                    sb.Append("<dl>");
                    for (int j = 0; j < ps[i].Children.Length; j++)
                    {
                        sb.Append("<dd><a href=\"productcatlist.aspx?c=" + c.ToString() + "&id=" + ps[i].Children[j].ID.ToString() + "\">" + ps[i].Children[j].Title + "</a></dd>");
                    }
                    sb.Append("</dl>");
                }
            }
        }
        this.ltlLeftNav.Text = sb.ToString();
    }

    private void InitList(int parent)
    {
        InitPath(parent);

        CProduct product = new CProduct();
        ProductInfo[] ps = product.ReadProductTree(parent);
        StringBuilder sb = new StringBuilder();
        if (null != ps)
        {
            sb.Append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >");
            for (int i = 0; i < ps.Length; i++)
            {
                if (i % 4 == 0)
                {
                    sb.Append("<tr>");
                }
                sb.Append("<td><a href=\"productdetail.aspx?c="+Request.QueryString["c"].ToString()+"&id="+ps[i].ID.ToString()+"\"><img width=\"170px\" src=\"" + ps[i].Image + "\" alt=\"\" /></a></td>");
                if ((i + 1) % 4 == 0)
                {
                    sb.Append("</tr>");
                }
            }
            sb.Append("</table>");
        }
        this.ltlContent.Text = sb.ToString();
    }

    private void InitPath(int id)
    {
        CProduct p = new CProduct();
        List<string[]> lst = p.GetProductPath(id);
        StringBuilder sb = new StringBuilder();
        if (null != lst)
        {
            sb.Append(lst.Last()[1]);
            if (lst.Count > 2)
            {
                for (int i = 1; i <= lst.Count - 2; i++)
                {
                    sb.Insert(0, "<a href=\"productcatlist.aspx?c=" + Request.QueryString["c"].ToString() + "&id=" + lst[i][0] + "\">" + lst[i][1] + "</a> > ");
                }
            }
            sb.Insert(0, "<a href=\"productcatlist.aspx?c=" + Request.QueryString["c"].ToString() +"\">" + lst[0][1] + "</a> > ");
        }
        this.ltlTopNav.Text = sb.ToString();
    }
}